package com.service.wx;

import com.bean.wx.ResultInfo;
import com.bean.wx.UserInfoWx;
import com.mapper.repositorywx.UserInfoWxRepository;
import com.utils.wx.WxReplyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

@Service
public class WxServiceImpl implements WxService {

    //判断是事件还是消息
    @Override
    public String replyMessage(Map<String,String> map) {
        String msgType=map.get("MsgType");
        String result="";
        if(!"event".equals(msgType)){
            try {
                result=message(map);//处理消息事件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                result = event(map);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }



    @Autowired
    private WxReplyMessage wxReplyMessage;

    private String replyMessage(ResultInfo resultInfo){
        return wxReplyMessage.replyMessage(resultInfo,resultInfo.getReturnMethod());
    }

    //处理消息
    private String message(Map<String,String> map) throws IOException {
        String result="";
        ResultInfo resultInfo=new ResultInfo();
        resultInfo.setFromUserName(map.get("ToUserName"));
        resultInfo.setToUserName(map.get("FromUserName"));
        resultInfo.setCreteTime(wxReplyMessage.getUtcTime());
        String msgType=map.get("MsgType");
        if("text".equals(msgType)){//普通消息
            String content=map.get("Content");
            if(content.equals("个人系统")){
                //回复图文信息
                //result=function1019(resultInfo);
                result = returnSystem(resultInfo);
            }
            if(content.equals("随机音乐")){
                //回复图文信息
                //result=function1019(resultInfo);
                result = returnSjmusic(resultInfo);
            }
        }
        return result;
    }

    private String event(Map<String,String> map) throws IOException {
        String result="";
        ResultInfo resultInfo=new ResultInfo();
        resultInfo.setFromUserName(map.get("ToUserName"));
        resultInfo.setToUserName(map.get("FromUserName"));
        resultInfo.setCreteTime(wxReplyMessage.getUtcTime());
        String event=map.get("Event");
        if("subscribe".equals(event)){
            result = returnSubScribe(resultInfo);
        }
        if("unsubscribe".equals(event)){
            result = returnUnSubScribe(resultInfo);
        }
        return result;
    }

    @Autowired
    private UserInfoWxRepository userInfoWxRepository;

    @Value("${server.ip}")
    private String server;

    @Value("${server.http.port}")
    private String port;

    @Override
    public String returnSystem(ResultInfo resultInfo) {

        resultInfo.setReturnMethod("news");
        resultInfo.setTitle("个人信息系统");
        resultInfo.setDescription("管理个人的信息等内容,“请勿转发!!!!”“仅限个人阅读!!!”");
        resultInfo.setUrl("http://"+server+":"+port+"/?openid="+resultInfo.getToUserName());
        return replyMessage(resultInfo);
    }

    public String returnSjmusic(ResultInfo resultInfo) {

        resultInfo.setReturnMethod("news");
        resultInfo.setTitle("随机音乐");
        resultInfo.setDescription("基于网易随机音乐API");
        resultInfo.setUrl("http://"+server+":"+port+"/music.html");
        return replyMessage(resultInfo);
    }


    //订阅后，自动注册，并添加用户信息
    @Override
    public String returnSubScribe(ResultInfo resultInfo) {
        UserInfoWx userInfo = new UserInfoWx();
        userInfo.setId(UUID.randomUUID().toString());
        userInfo.setOpenid(resultInfo.getToUserName());
        userInfo.setStatus("subscribe");
        userInfo.setCreatetime(LocalDateTime.now());
        userInfoWxRepository.save(userInfo);

        resultInfo.setReturnMethod("text");
        resultInfo.setContent("通过您的关注为你注册了，个人系统。回复：‘个人系统’可获取链接查看");

        return replyMessage(resultInfo);
    }

    //取消关注
    public String returnUnSubScribe(ResultInfo resultInfo) {



        UserInfoWx userInfo = userInfoWxRepository.findByOpenid(resultInfo.getToUserName());
        userInfo.setUpdatetime(LocalDateTime.now());
        userInfo.setStatus("unsubscribe");
        userInfoWxRepository.save(userInfo);

        return replyMessage(resultInfo);
    }

    //处理事件
   /* private String event(Map<String,String> map){
        String result="";
        String event=map.get("Event");
        ResultInfo resultInfo=new ResultInfo();
        resultInfo.setFromUserName(map.get("ToUserName"));
        resultInfo.setToUserName(map.get("FromUserName"));
        resultInfo.setCreteTime(wxReplyMessage.getUtcTime());
        if("CLICK".equals(event)&&map.get("EventKey")!=null){

            if("BWL".equals(map.get("EventKey"))){
                BWL(resultInfo);
            }
            if("DC".equals(map.get("EventKey"))){
                DC(resultInfo);
            }
            result=replyMessage(resultInfo);
        }

        return result;
    }*/



    //点击事件--备忘录
/*    private void BWL(ResultInfo resultInfo){
        resultInfo.setReturnMethod("news");
        resultInfo.setTitle("测试");
        resultInfo.setDescription("这个不能转发哦!!!");
        resultInfo.setUrl(natappUrl+"/memo.html?userid="+resultInfo.getToUserName());
    }*/

    //点击事件--点餐
/*    private void DC(ResultInfo resultInfo){
        resultInfo.setReturnMethod("news");
        resultInfo.setTitle("菜单");
        resultInfo.setDescription("这个不能转发哦!!!");
        resultInfo.setUrl(natappUrl+"/menu.html?userid="+resultInfo.getToUserName());
    }*/

/*    private void DD(ResultInfo resultInfo){
        resultInfo.setReturnMethod("news");
        resultInfo.setTitle("订单");
        resultInfo.setDescription("转发这个给你的初始哦!!!");
        resultInfo.setUrl(natappUrl+"/memo.html?userid="+resultInfo.getToUserName());
    }*/

}
